﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using iConnector;
using System.IO;

namespace Tester
{
    class Manager : IPluginHost
    {
        private iPlugin[] ipi;
        public void RetrievePlugins()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory; //Application.StartupPath;
            string[] pluginFiles = Directory.GetFiles(path, "*.dll");
            ipi = new iPlugin[pluginFiles.Length];

            for (int i = 0; i < pluginFiles.Length; i++)
            {
                string args = pluginFiles[i].Substring(
                    pluginFiles[i].LastIndexOf(@"\") + 1,
                    pluginFiles[i].IndexOf(".dll") -
                    pluginFiles[i].LastIndexOf(@"\") - 1);
                Type ObjType = null;
                try
                {
                    // load it
                    Assembly ass = null;
                    ass = Assembly.Load(args);
                    if (ass != null)
                    {
                        ObjType = ass.GetType(args + ".Plugin");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("While Getting Type: " + ex.Message);
                }
                try
                {
                    // OK Lets create the object as we have the Report Type
                    if (ObjType != null)
                    {
                        ipi[i] = (iPlugin)Activator.CreateInstance(ObjType);
                        ipi[i].Host = this;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("On Activator: " + ex.Message);
                }
            }
        }
        public void LoadPlugin(string PluginName)
        {
            int PluginID = 0;
            Boolean PluginIDFound = false;

            for (int i = 0; i < ipi.Length; i++)
            {
                if (ipi[i] != null)
                {
                    if (ipi[i].Name.ToLower() == PluginName.ToLower())
                    {
                        PluginID = i;
                        PluginIDFound = true;
                    }
                }
            }
            if (PluginIDFound)
            {
                Console.WriteLine("Loading: " + ipi[PluginID].Name);
                ipi[PluginID].Load();
            }
            else
            {
                Console.WriteLine("Plugin Not Found");
            }
        }
        public void LoadPlugins()
        {
            for (int i = 0; i < ipi.Length; i++)
            {
                if (ipi[i] != null)
                {
                    Console.WriteLine("Loading: " + ipi[i].Name);
                    ipi[i].Load();
                }
            }
        }
      
        public bool Register(iPlugin ipi)
        {
            //MenuItem mn = new MenuItem(ipi.Name, new EventHandler(NewLoad));
            Console.WriteLine("Registered Plugin: " + ipi.Name);
            //menuItem1.MenuItems.Add(mn);
            return true;
        }
        
    }
    class Moduler : IModuleHost
    {
        private iModule[] imo;
        
        public void RetrieveModules()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory; //Application.StartupPath;
            string[] pluginFiles = Directory.GetFiles(path, "*.dll");
            imo = new iModule[pluginFiles.Length];

            for (int i = 0; i < pluginFiles.Length; i++)
            {
                string args = pluginFiles[i].Substring(
                    pluginFiles[i].LastIndexOf(@"\") + 1,
                    pluginFiles[i].IndexOf(".dll") -
                    pluginFiles[i].LastIndexOf(@"\") - 1);
                Type ObjType = null;
                try
                {
                    // load it
                    Assembly ass = null;
                    ass = Assembly.Load(args);
                    if (ass != null)
                    {
                        ObjType = ass.GetType(args + ".Module");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("While Getting Type: " + ex.Message);
                }
                try
                {
                    // OK Lets create the object as we have the Report Type
                    if (ObjType != null)
                    {
                        imo[i] = (iModule)Activator.CreateInstance(ObjType);
                        imo[i].Host = this;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("On Activator: " + ex.Message);
                }
            }
        }
        public void LoadModules()
        {
            for (int i = 0; i < imo.Length; i++)
            {
                if (imo[i] != null)
                {
                    Console.WriteLine("Loading: " + imo[i].Name);
                    imo[i].Load();
                }
            }
        }
        public void UnLoadModules()
        {
            for (int i = 0; i < imo.Length; i++)
            {
                if (imo[i] != null)
                {
                    Console.WriteLine("UnLoading: " + imo[i].Name);
                    imo[i].Unload();
                }
            }
        }
        public bool Register(iModule imo)
        {
            Console.WriteLine("Registered Module: " + imo.Name);
            return true;
        }
    }
}
